package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class AddressController {
    @Autowired //注入
  private  AddressService addressService;

    //地址列表
    @GetMapping("/address/list")
    public ResultInfo findList(){
        //调用service查询并返回
        return ResultInfo.success(addressService.findList());
    }
    //地址新增
    @PostMapping("/address")
    public ResultInfo save(@RequestBody Address address){ //接收请求参数
       //调用service新增
        addressService.save(address);
        //并返回
       return ResultInfo.success();
    }
    //设为默认地址
    @PutMapping("/address/default")
    public ResultInfo setDefault(@RequestBody Map<String,Long> param ){
    //接收请求参数
        Long id = param.get("id");
        //调用service设置
        addressService.setDefault(id);
        //返回结果
        return ResultInfo.success();
    }
    //去结算,查询默认地址
    @GetMapping("/address/default")
    public ResultInfo findDefault(){ //接收请求
        //调用service查询
       Address address =  addressService.findDefault();
       //返回结果
        return ResultInfo.success(address);
    }
    //回显地址
    @GetMapping("/address/{id}")
    public ResultInfo findById(@PathVariable Long id){ //接收请求
        //调用service查询
      Address address =   addressService.findById(id);
      //返回
        return ResultInfo.success(address);

    }
    //修改地址
    @PutMapping("/address")
    public ResultInfo updateById(@RequestBody Address address){
        //调用service修改
        addressService.updateById(address);
        //返回结果
        return ResultInfo.success();
    }
    //删除地址
    @DeleteMapping("/address")
    public ResultInfo deleteById(Long ids){ //接收请求参数
    //调用service删除
    addressService.deleteById(ids);
    //返回结果
     return ResultInfo.success();

    }
}

